﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Configuration;

namespace Util
{
    public static class VietnameseText
    {
        const string FindText = ".áàảãạâấầẩẫậăắằẳẵặđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵÁÀẢÃẠÂẤẦẨẪẬĂẮẰẲẴẶĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ'";
        const string ReplText = " aaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAADEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYY ";

        /// <summary>
        /// Convert to unmark text.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToUpText(this string s)
        {
            int index = -1;
            while ((index = s.IndexOfAny(FindText.ToCharArray())) != -1)
            {
                int index2 = FindText.IndexOf(s[index]);
                s = s.Replace(s[index], ReplText[index2]);
            }
            s = s.Replace(' ', '-').Replace('&', '-');
            return Regex.Replace(s, @"[\\\/:\*\?""<>|]", string.Empty);
        }
    }

    public class ControlHelper
    {
        //public delegate void Init<T>(T t);

        //public static string ControlToString<T>(string path, Init<T> init) where T : UserControl
        //{
        //    try
        //    {
        //        T t = null;
        //        t = ((Page)HttpContext.Current.Handler).LoadControl(path) as T;
        //        init(t);
        //        StringBuilder result = new StringBuilder(1024);
        //        t.RenderControl(new HtmlTextWriter(new StringWriter(result)));
        //        return result.ToString();
        //    }
        //    catch { return string.Empty; }
        //}
    }

    public static class HtmlUtil
    {
        public static string RemoveAllHtmlTag(string content)
        {
            string result = content;
            string[] removeTagArray = new string[] { "b", "a", "script", "i", "ul", "li", "ol", "font", "span", "div", "u" };
            foreach (string removeTag in removeTagArray)
            {
                string regExpressionToRemoveBeginTag = string.Format("<{0}([^>]*)>", removeTag);
                Regex regEx = new Regex(regExpressionToRemoveBeginTag, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                result = regEx.Replace(result, "");

                string regExpressionToRemoveEndTag = string.Format("</{0}([^>]*)>", removeTag);
                regEx = new Regex(regExpressionToRemoveEndTag, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                result = regEx.Replace(result, "");
            }
            return result;
        }
    }
}